//
//  AlarmDisposeDetailVC.m
//  efairy
//  报警处理详情
//  Created by 李锋 on 2019/2/27.
//  Copyright © 2019年 East. All rights reserved.
//

#import "AlarmDisposeDetailVC.h"
#import "EFAModel.h"
#import "ApiManager.h"
#import "AlarmDisposeDetailModel.h"
#import "GlobalHeader.h"
#import "AlarmDisposeDetailCell.h"
#import "KSPhotoBrowser.h"

@interface AlarmDisposeDetailVC ()<UITableViewDelegate,UITableViewDataSource,AlarmDisposeDetailCellDelegate>
@property(nonatomic,strong) AlarmDisposeDetailModel *infoDetail;
@end

@implementation AlarmDisposeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationbar];
    [self initView];
    [self requestAlarmInfo];
}

-(void)setNavigationbar{
    self.navigationItem.title = @"报警处理详情";
}

-(void)initView{
    float  navigationbarH = self.navigationController.navigationBar.frame.size.height;
    
    self.view .backgroundColor =  ColorFromRGB(0xeff0f4);
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,statusBarHeight+navigationbarH, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.backgroundColor = ColorFromRGB(0xeff0f4);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = ColorFromRGB(0xeff0f4);//设置分割线颜色
    //多余分割线隐藏
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _infoDetail ? 1 :0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        AlarmDisposeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"AlarmDisposeDetailCell" description]];
        if(!cell){
            cell = [[AlarmDisposeDetailCell alloc]init];
           
        }
        //禁止cell被点击
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        [cell setModelDatas:_infoDetail];
    
        // 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    
        return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH tableView:tableView];
    //    return [tableView cellHeightForIndexPath: indexPath];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor whiteColor];
}

//接受点击cell item项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - private methods
- (void)showPhotoBrowser:(NSArray<KSPhotoItem *> *)photoItems selectedIndex:(NSUInteger)index {
    KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:photoItems selectedIndex:index];
    browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleNone;
    browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlack;
    browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
    browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
    browser.bounces = NO;
    [browser showFromViewController:self];
}

#pragma mark - AlarmDisposeDetailCellDelegate
- (void)devInfoImgTCell:(AlarmDisposeDetailCell *)cell imgViewTapped:(NSArray<UIImageView *> *)imgViewArray indexPath:(NSIndexPath *)cIndexPath {
    if (cIndexPath.row >= _infoDetail.efairyalarmrecordimage_list.count) {
        return;
    }
    
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:_infoDetail.efairyalarmrecordimage_list.count];
    [_infoDetail.efairyalarmrecordimage_list enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *tempImgView = nil;
        if (idx < imgViewArray.count) {
            tempImgView = imgViewArray[idx];
        }
        KSPhotoItem *photoItem = [KSPhotoItem itemWithSourceView:tempImgView imageUrl:[NSURL URLWithString:obj]];
        [mArray addObject:photoItem];
    }];
    [self showPhotoBrowser:mArray selectedIndex:cIndexPath.row];
}

#pragma mark - network request
- (void)requestAlarmInfo {
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        return;
    }
    NSMutableDictionary *pars = [NSMutableDictionary dictionary];
    [pars setObject:@([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id) forKey:@"efairyuser_id"];
    [pars setObject:[EFAModel sharedInstance].loginInfo.access_token forKey:@"access_token"];
    //报警编号
    [pars setObject:self.alarmId forKey:@"efairydevicealarmstatistics_id"];
    
    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    [[XDAFNetworking sharedInstance] GET:[ApiManager apiAlarmDispose] parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求已处理报警信息：%@", responseObject);
        [hud hideAnimated:NO];
        //把服务端返回的json格式数据解析为对象
        _infoDetail = [AlarmDisposeDetailModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求设备列表失败：%@", error);
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}


@end
